﻿using DataType.Actions;
using DataType.JudgeResults;
using OprsSimulator.Operators;
using System.Collections.Generic;
using MapCalclator;
using System;
using System.Diagnostics;
using DataType.CustomEnumType;
using OprsSimulator.OprAttributes;

namespace ActionsJudge
{
    internal static class StopMoveJudge
    {
        public static List<IJudgeResult> ToJudge(IBasicAction acts, List<IOperator> oprs, Map map)
        {
            List<IJudgeResult> judgeResults = new List<IJudgeResult>();// 保存裁决结果，用于返回

            StopMoveAction action = (StopMoveAction)acts;// 转为停止机动的动作类型
            IOperator opr = null;// 寻找执行动作的算子
            foreach (IOperator oper in oprs)
            {
                if (oper.ObjID == action.ObjID)
                {
                    opr = oper;
                    break;
                }
            }
            if (opr == null)
            {
                Trace.TraceWarning($"无效裁决：态势中没有ID为{action.ObjID}的算子");
                return new List<IJudgeResult>();
            }
            if (action.Type != ActionType.StopMove)
            {
                Trace.TraceWarning("无效裁决：算子的动作类型有误，应该是停止机动类型");  
                return new List<IJudgeResult>();
            }

            if (!typeof(ICommonAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IMoveAttris).IsAssignableFrom(opr.GetType()))
            {
                Trace.TraceWarning($"无效裁决：算子{action.ObjID}不具有停止机动所需要的全部属性");
                return new List<IJudgeResult>();
            }

            int speed = (int)opr.GetType().GetProperty("Speed").GetValue(opr);

            bool stop_move_flag = false;// 可以停止机动的标志位
            if (speed > 0)// 速度大于0则可以停止机动
            {
                stop_move_flag = true;
            }

            // 生成裁决结果，添加进judgeResults
            StopMoveJudgeResult stopMoveJudgeResult = new StopMoveJudgeResult(action.ObjID, stop_move_flag);
            judgeResults.Add(stopMoveJudgeResult);
            
            return judgeResults;
        }
    }
}
